/**
 * 染膏机蓝牙B协议，4g + 蓝牙通道
 */
import agreeb from './agreeB.js'

/**
 * @param {Object} upData
 * @return {"mac":"","colorNo":123}
 */
const verifyBRgj = (upData) => {
	let responseVo = agreeb.verify(upData)
	if (responseVo.code != 0) {
		return responseVo
	}
	let agreeBInfo = responseVo.data
	let hexParam = agreeBInfo.hexParam
	// console.log("hexParam=========" + hexParam);
	let dataObj = {
		mac: agreeBInfo.mac,
		param: hexParam
	}
	return dataObj
}

/**
 * 下料协议
 * 66+00(备用)+0000(命令2字节)+MAC(6字节)+系统时间(6字节)+副机号(2字节)+副机总数(2字节)+行号(2字节)+状态标志(2字节)+参数长度(2字节)+参数+累加取反(1字节)
 * 示例数据 66 00 00B0 000100000058 220218133147 0000 0000 0000 0000
 * 000F 01100033000306000103E9001E7A08 E0
 * 66 00 00B0000100000058 220218133147 0000 0000 0000 0000 000d 01100033000306000103e9001e7a08 1d
 * @param {Object} mac
 * @param {Object} mode 1：智能模式，2：畅想模式
 * @param {Object} colorNo 色卡编号选择 模式=1时，范围0 ~9999。模式=2时，范围1 ~6
 * @param {Object} useAmount 用量 模式=1时，范围30~120，需设置30的倍数，模式=2时，范围1~100
 */
const getSetColor = (mac, mode, colorNo, useAmount, commitId) => {
	if (mode == (null || undefined)) {
		return ''
	}
	if (colorNo == (null || undefined)) {
		return ''
	}
	if (useAmount == (null || undefined)) {
		return ''
	}
	let hexMode = agreeb.intToHex(mode)
	hexMode = hexMode.padStart(4, '0')
	let hexColorNo = agreeb.intToHex(colorNo)
	hexColorNo = hexColorNo.padStart(4, '0')
	let hexUseAmount = agreeb.intToHex(useAmount)
	hexUseAmount = hexUseAmount.padStart(4, '0')
	let param = '01100033000306' + hexMode + hexColorNo + hexUseAmount
	// console.log("getSetColor=============hexMode=" + hexMode + " hexColorNo=" + hexColorNo + " hexUseAmount=" +
	// 	hexUseAmount);
	let data = agreeb.down(mac, '00B0', param, commitId, 'CRC')
	console.log('getSetColor=============data=' + data.toUpperCase())
	return data
}

/**
 * 下料协议
 * 66+00(备用)+0000(命令2字节)+MAC(6字节)+系统时间(6字节)+副机号(2字节)+副机总数(2字节)+行号(2字节)+状态标志(2字节)+参数长度(2字节)+参数+累加取反(1字节)
 * @param {Object} mac
 * @param {Object} mode 1：智能模式，2：畅想模式
 * @param {Object} colorNo 色卡编号选择 模式=1时，范围0 ~9999。模式=2时，范围1 ~6
 * @param {Object} useAmount 用量 模式=1时，范围30~120，需设置30的倍数，模式=2时，范围1~100
 * @param {*} cList 几个头 [{1:1.9},{3:10},{5:19.1}] (小数部分只精确到一位)
 */
const getSetColor2 = (mac, mode, colorNo, useAmount, commitId, cList) => {
	if (mode == (null || undefined)) {
		return ''
	}
	if (colorNo == (null || undefined)) {
		return ''
	}
	if (useAmount == (null || undefined)) {
		return ''
	}

	let hexMode = agreeb.intToHex(mode)
	let oxydolAmount_ = 0
	let totalAmount = useAmount
	hexMode = hexMode.padStart(4, '0')
	let hexColorNo = agreeb.intToHex(colorNo)
	hexColorNo = hexColorNo.padStart(4, '0')

	// 结束双氧计算
	let count = 0
	let cParam = ''
	cList.forEach((item) => {
		Object.keys(item).forEach((key) => {
			let hexI = agreeb.intToHex(parseInt(key))
			hexI = hexI.padStart(2, '0')
			let tiemVal = (item[key] + '').split('.')
			let inter = 0
			let floter = 0
			if (tiemVal.length > 1) {
				inter = parseInt(tiemVal[0])
				floter = parseInt(tiemVal[1])
			} else {
				inter = parseInt(tiemVal[0])
			}

			let hexInter = agreeb.intToHex(inter)
			let hexFloter = agreeb.intToHex(floter)

			hexInter = hexInter.padStart(2, '0')
			hexFloter = hexFloter.padStart(2, '0')
			let hexV = hexInter + hexFloter

			cParam += hexI + hexV
			count++
		})
	})
	totalAmount = parseFloat(useAmount) + parseFloat(oxydolAmount_)
	let hexUseAmount = agreeb.intToHex(parseInt(totalAmount))
	hexUseAmount = hexUseAmount.padStart(4, '0')
	let hexCount = agreeb.intToHex(count)
	hexCount = hexCount.padStart(2, '0')
	console.log(mac, mode, colorNo, useAmount, commitId)
	console.log('cList====================================> ', cList)
	let countP = 6 + 1 + count * 3
	let hexCountP = countP.toString(16)
	hexCountP = hexCountP.padStart(2, '0')
	let param = '011000330003' + hexCountP + hexMode + hexColorNo + hexUseAmount + hexCount + cParam
	let data = agreeb.down(mac, '00C0', param, commitId, 'CRC')
	console.log('getSetColor2====================================>' + data.toUpperCase())
	return data
}

/**
 * 换料
 * @param {Object} mac
 * @param {Object} val 0：无效，1：换料完成，继续运行，2：不换料直接退出。
 */
const getReloadColor = (mac, val) => {
	mac = agreeb.padLeft(mac, '0', 12)
	let hexVal = parseInt(val).toString(16)
	hexVal = hexVal.padStart(4, '0')
	let param = '01060037' + hexVal
	let data = agreeb.down(mac, '00B0', param, '', 'CRC')
	return data
}

export default {
	getSetColor,
	getSetColor2,
	agreeb,
	verifyBRgj,
	getReloadColor
}
